package cn.kgc.tangcco.action;

import cn.binarywang.tools.generator.ChineseIDCardNumberGenerator;
import cn.binarywang.tools.generator.ChineseMobileNumberGenerator;
import cn.binarywang.tools.generator.ChineseNameGenerator;
import cn.kgc.tangcco.pojo.Person;
import cn.kgc.tangcco.utils.servlet.BaseServlet;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/10/20 上午10:30
 */
@WebServlet(urlPatterns = "/person.action", loadOnStartup = 1)
public class PersonAction extends BaseServlet {
    private static final long serialVersionUID = -4747540820232309556L;
    private static List<Person> personList = new ArrayList<>();

    static {
        for (int i = 0; i < 10; i++) {
            String mobile = ChineseMobileNumberGenerator.getInstance().generate();
            String nickname = ChineseNameGenerator.getInstance().generate();
            String idCard = ChineseIDCardNumberGenerator.getInstance().generate();
            personList.add(new Person(i + 1, mobile, nickname, "123456", idCard));
        }
        personList.forEach(person -> System.out.println(JSON.toJSONString(person)));
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mobile = request.getParameter("mobile");
        String authString = request.getParameter("auth_string");
        if (StringUtils.isBlank(mobile)) {
            System.out.println("账号不能为空");
            redirect(request, response, "index.jsp");
            return;
        } else if (StringUtils.isBlank(authString)) {
            System.out.println("密码不能为空");
            redirect(request, response, "index.jsp");
            return;
        } else {
            ListIterator<Person> it = personList.listIterator();
            while (it.hasNext()) {
                Person person = it.next();
                if (person.getMobile().equals(mobile) && person.getAuthString().equals(authString)) {
                    System.out.println(JSON.toJSONString(person));
                    // 将
                    person.setAuthString("");
                    //
                    request.getSession().setAttribute("operator", person);
                    // 跳转页面
                    forwordTable(request,response);
                    break;
                }
            }
            redirect(request, response, "index.jsp");
            return;
        }
    }
    public void forwordTable(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("persons",personList);
        forwordToWEB_INF_SuffixJSP(request, response, "table");
    }

    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();
        redirect(request, response, "index.jsp");
        return;
    }
    public void test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mobile = ChineseMobileNumberGenerator.getInstance().generate();
        String nickname = ChineseNameGenerator.getInstance().generate();
        String idCard = ChineseIDCardNumberGenerator.getInstance().generate();
        Person person = new Person(1, mobile, nickname, "123456",idCard);
        request.setAttribute("operator",person);
        forwordSuffixJSP(request,response,"/demo11");
        return;
    }
}
